package com.dhcc.bpm.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.common.text.Convert;
import com.dhcc.bpm.modules.alarm.entity.Alarm;
import com.dhcc.bpm.modules.alarm.service.IAlarmService;
import com.dhcc.bpm.modules.orga.entity.Orga;
import com.dhcc.bpm.modules.orga.entity.Userhome;
import com.dhcc.bpm.modules.orga.service.IOrgaService;
import com.dhcc.bpm.modules.orga.service.IUserhomeService;
import com.dhcc.bpm.modules.system.entity.*;
import com.dhcc.bpm.modules.system.service.*;
import com.dhcc.bpm.modules.system.vo.UserVO;
import com.dhcc.bpm.modules.system.wrapper.UserWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.springblade.core.cache.constant.CacheConstant.USER_CACHE;
/**
 * @ClassName UserController
 * @Description 用户 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@ApiIgnore
@RestController
@RequestMapping("/blade-user")
@AllArgsConstructor
public class UserController {
	private  static final Logger log = LoggerFactory.getLogger(UserController.class);
	private IUserService userService;

	private IUserRoleService userRoleService;

	private IUserDeptService userDeptService;

	private IRoleService roleService;

	private IAlarmService alarmService;

	private IDeptService deptService;

	private IUserhomeService userhomeService;

	private IOrgaService orgaService;

	private ISerialNumberService serialNumberService;

	/**
	 * @Description 查询单条
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.UserVO>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "查看详情", notes = "传入id")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@ApiLog("查询用户详情")
	@Log(title = "查询用户详情", businessType = BusinessType.SELECT)
	public R<UserVO> detail(User user) {
		User detail = userService.getOne(Condition.getQueryWrapper(user));
		return R.data(UserWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 查询单条
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.system.vo.UserVO>
	 **/
	@GetMapping("/info")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查看详情", notes = "传入id")
	//@PreAuth("hasAnyRole('administrator', 'user','test')")
	@ApiLog("查询用户详情")
	@Log(title = "查询用户详情", businessType = BusinessType.SELECT)
	public R<UserVO> info(BladeUser user) {
		User detail = userService.getById(user.getUserId());
		return R.data(UserWrapper.build().entityVO(detail));
	}

	/**
	 * @Description 用户列表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param user
	 * @param query
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.UserVO>>
	 **/
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	@Log(title = "查询用户列表", businessType = BusinessType.SELECT)
	@ApiLog("查询用户列表")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	public R<IPage<UserVO>> list(@ApiIgnore @RequestParam Map<String, Object> user, Query query, BladeUser bladeUser) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
		IPage<User> pages = userService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId()) : queryWrapper);
		return R.data(UserWrapper.build().pageVO(pages));
	}

	/**
	 * @Description 用户列表
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param user
	 * @param query
	 * @param bladeUser
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.UserVO>>
	 **/
	@GetMapping("/listUser")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
			@ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入account和realName")
	@Log(title = "查询用户列表", businessType = BusinessType.SELECT)
	@ApiLog("查询用户列表")
	public R<IPage<UserVO>> listUser(@ApiIgnore @RequestParam Map<String, Object> user, Query query, BladeUser bladeUser) {
		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
		IPage<User> pages = userService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId()) : queryWrapper);
		return R.data(UserWrapper.build().pageVO(pages));
	}
	/**
	 * @Description 根据邮箱查询用户
	 * @Author daina
	 * @Date 11:14 2020/3/28
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("根据邮箱查询用户")
	@PostMapping("/selectByEmail")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "根据邮箱查询用户", notes = "email")
	@Log(title = "根据邮箱查询用户",businessType = BusinessType.SELECT)
	public R selectByEmail(@Valid @RequestBody User user){
		User userEntity = userService.selectByEmail(user);
		if(userEntity != null){
			if(null==user.getId()){
				return R.data(false);
			}else{
				if(!user.getId().equals(userEntity.getId())){
					return R.data(false);
				}
			}

		}
		return R.data(true);
	}
	/**
	 * @Description 查询用户手机号
	 * @Author daina
	 * @Date 11:44 2020/3/28
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("查询用户手机号")
	@PostMapping("/selectByPhone")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询用户手机号", notes = "email")
	@Log(title = "查询用户手机号",businessType = BusinessType.SELECT)
	public R selectByPhone(@Valid @RequestBody User user){
		User userEntity = userService.selectByPhone(user);
		if(userEntity != null){
			if(null==user.getId()){
				return R.data(false);
			}else{
				if(!user.getId().equals(userEntity.getId())){
					return R.data(false);
				}
			}

		}
		return R.data(true);
	}


	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:09 2020/2/22 0022
	 * @param userVO
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入User")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@CacheEvict(cacheNames = {USER_CACHE}, allEntries = true)
	@ApiLog("新增或修改用户")
	@Log(title = "用户", businessType = BusinessType.INSERT)
	@Transactional(rollbackFor = Exception.class)
	public R submit(@Valid @RequestBody UserVO userVO) {
		Long userId = userVO.getId();
		//新增用户
		boolean userInsert = userService.submit(userVO);
		if (userInsert == true) {
			User byaccount = userService.selectUserByaccount(userVO.getAccount());
			//添加用户角色中间表信息
			boolean userRole = userRoleService.batchUserRole(byaccount.getId(), userVO.getRoleId());
			//添加用户部门中间表信息
			boolean userDept = userDeptService.batchUserDept(byaccount.getId(), userVO.getDeptId());
			//添加上级角色
			User userRoles = new User();
			String roleIds = userVO.getRoleId();
			userRoles.setId(byaccount.getId());
			//用户角色选择那个添加那个（选择子级未选择父级不用添加父级） 修改时间20200924 11:28:00
//			userRoles.setRoleId(selectRole(roleIds));
			userRoles.setRoleId(roleIds);
			userService.updateUser(userRoles);

			//增加内部门户用户 修改时间：20200922 22:55:0
			// 管理端用户关联子部门时，创建的门户用户关联该子部门的父部门所对应的机构
			if(0 == userVO.getIsDeleted() || 2 == userVO.getIsDeleted()){
				List<Userhome> userhomeList = userhomeService.findOpcUser(userVO.getEmail(), userVO.getPhone());
				if(userId == null && userhomeList.size() > 0){
					userService.removeByIds(Arrays.asList(userVO.getId()));
					return R.fail(222, "该手机号或邮箱已被门户其他用户绑定，请更换手机号和邮箱");
				}
				Userhome userhome = userhomeList.size() > 1 ? userhomeList.get(0) : null;
				Dept dept = deptService.getById(userVO.getDeptId());
				String[] ancestors = dept.getAncestors().split(",");
				String deptId;
				if (ancestors.length == 1) {
					deptId = String.valueOf(dept.getId());
				} else {
					deptId = ancestors[1];
				}

				boolean opcUserStatus = false;

				if(userhome == null){
					userhome = new Userhome();
				}

				userhome.setId(userVO.getId());
				userhome.setUserName(userVO.getName());
				userhome.setUserPassword(userVO.getPassword());
				userhome.setEmail(userVO.getEmail());
				userhome.setMobile(userVO.getPhone());
				userhome.setUserType("1");
				userhome.setUserStatus(1);
				userhome.setIsDeleted(0);
				userhome.setSpare4(deptId);
				try {
					opcUserStatus = userhomeService.save(userhome);
				}catch (Exception e){
					log.error("新增内部门户用户失败：{}", e);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return R.fail(222, "新增内部门户用户失败");
				}

				if (userRole && userDept && opcUserStatus && addOrga(userVO)) {
					return R.status(true);
				} else {
					return R.status(false);
				}
			}

			if (userRole && userDept) {
				return R.status(true);
			} else {
				return R.status(false);
			}
		} else {
			return R.status(false);
		}
	}

	/*
	 * @Description 管理端新增修改门户用户时给用户增加机构
	 * @Author xiafayu
	 * @Date 2021/3/9 16:23
	 * @param userVO
	 * @return boolean
	 * @throws
	 **/
	public boolean addOrga(User userVO){
		boolean flag = false;

		//创建门户用户时增加机构信息
		Orga orga = orgaService.findOrgaByDeptId(userVO.getDeptId());
		if(orga == null){
			orga = new Orga();
			orga.setId(Long.valueOf(userVO.getDeptId()));
			String orgaCode = serialNumberService.generateSerialNumberByModelCode("opc_orga_code");
			orga.setOrgaCard(orgaCode);
			orga.setRegTime(DateUtil.now());
			orga.setAccessTime(DateUtil.now());
			orga.setOrgaStatus("1");
			orga.setStatus(1);
			orga.setOperationType("1");
			orga.setAuthFlag("1");
			orga.setIsDeleted(0);
			orga.setOrgaName(deptService.getById(userVO.getDeptId()).getDeptName());
		}
		Orga orga1 = new Orga();
		BeanUtils.copyProperties(orga, orga1);
		orga1.setOpcUserId(userVO.getId());
		orga1.setId(userVO.getId());
		flag = orgaService.save(orga1);

		return flag;
	}

	private String selectRole(String roleIds) {
		Long[] roles = Convert.toLongArray(roleIds);
		for (Long data : roles){
			Role role = roleService.getById(data);
			if (role.getParentId() != 0L){
				if (roleIds.contains(String.valueOf(role.getParentId()))){
					continue;
				}else{
					roleIds = roleIds + "," + String.valueOf(role.getParentId());
				}
			}
		}
		return roleIds;
	}

	/**
	 * @Description 修改
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入User")
	@CacheEvict(cacheNames = {USER_CACHE}, allEntries = true)
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@ApiLog("修改用户")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	@Transactional(rollbackFor = Exception.class)
	public R update(@Valid @RequestBody User user) {
		if((user.getIsDeleted() == 0 || user.getIsDeleted() == 2) && userhomeService.getById(user.getId()) == null && userhomeService.findOpcUser(user.getEmail(), user.getPhone()).size() > 0){
			return R.fail(222, "该手机号或邮箱已被门户其他用户绑定，请更换手机号和邮箱");
		}

		//修改用户角色中间表 和用户部门中间表信息
		List<UserRole> list =  userRoleService.selectUserByUserId(user.getId());


		List<UserDept> listDept =  userDeptService.selectDeptByUserId(user.getId());
		for(UserRole data : list){
			userRoleService.removeByUserId(data.getUserId());
		}
		for(UserDept dataDept : listDept){
			userDeptService.removeByUserId(dataDept.getUserId());
		}
		userRoleService.batchUserRole(user.getId(),user.getRoleId());
		userDeptService.batchUserDept(user.getId(),user.getDeptId());
		//添加上级角色
		String roleIds = user.getRoleId();
		//用户角色选择那个添加那个（选择子级未选择父级不用添加父级） 修改时间20200924 11:28:00
//		user.setRoleId(selectRole(roleIds));
		user.setRoleId(roleIds);

		boolean bool = false;
		try {
			bool = updateUser(user);
		}catch (SQLException e){
			log.error("修改内部门户用户信息失败:{}", e);
			return R.fail(222, "修改内部门户用户信息失败");
		}
		return R.status(bool && userService.updateUser(user));
	}

	/**
	 * @Description 修改用户生成门户用户
	 * @Author xiafayu
	 * @Date 2020/9/23 10:59
	 * @param user
	 * @return boolean
	 * @throws
	 **/
	@Transactional(rollbackFor = Exception.class)
	public boolean updateUser(User user) throws SQLException {
		//内部门户用户更新 修改时间:20200922 22:51:00
		if (user.getIsDeleted() == 0 || user.getIsDeleted() == 2) {
			boolean bool = false;
			Userhome userhome = userhomeService.getById(user.getId());
			if(userhome == null){
				userhome = new Userhome();
				userhome.setId(user.getId());
				userhome.setIsDeleted(0);
				userhome.setUserStatus(1);
				userhome.setUserType("1");
				bool = true;
			}

			Dept dept = deptService.getById(user.getDeptId());
			String[] ancestors = dept.getAncestors().split(",");
			String deptId;
			if (ancestors.length == 1) {
				deptId = String.valueOf(dept.getId());
			} else {
				deptId = ancestors[1];
			}
			userhome.setSpare4(deptId);
			userhome.setUserName(user.getName());
			userhome.setEmail(user.getEmail());
			userhome.setMobile(user.getPhone());
			if(userService.getById(user.getId()) != null){
				userhome.setUserPassword(userService.getById(user.getId()).getPassword());
			}

			boolean opcUserStatus = false;
			if(bool){
				opcUserStatus = userhomeService.save(userhome);
			}else{
				opcUserStatus = userhomeService.updateById(userhome);
			}

			//修改管理端门户用户时增加机构信息
			Orga orga = orgaService.findOrgaByUserId(user.getId());
			if(orga == null){
				return opcUserStatus && addOrga(user);
			}

			return opcUserStatus;
		}
		return true;
	}
	/**
	 * @Description 删除
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "删除", notes = "传入id集合")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@CacheEvict(cacheNames = {USER_CACHE}, allEntries = true)
	@ApiLog("删除用户")
	@Log(title = "用户", businessType = BusinessType.DELETE)
	public R remove(@RequestParam @NotEmpty String ids) {
		//先删除用户角色中间表和用户部门表
		List<Long> list = Func.toLongList(ids);
		list.forEach(userId -> {
			userRoleService.removeByUserId(userId);
			userDeptService.removeByUserId(userId);
		});
		return R.status(userService.removeUser(ids));
	}

	/**
	 * @Description 设置菜单权限
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param userIds
	 * @param roleIds
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/grant")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@ApiLog("设置菜单权限")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	public R grant(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds,
				   @ApiParam(value = "roleId集合", required = true) @RequestParam String roleIds) {
		if ("".equals(userIds) && "".equals(roleIds)){
			return R.fail("请至少选择一个角色!");
		}else{
			boolean temp = userService.grant(userIds, roleIds);
			return R.status(temp);
		}


	}

	/**
	 * @Description 重置密码
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param userIds
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/reset-password")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "初始化密码", notes = "传入userId集合")
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@ApiLog("重置用户密码")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	public R resetPassword(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds,
						   @ApiParam(value = "重置密码", required = true) @RequestParam String resetPassword) {
		boolean temp = userService.resetPassword(userIds,resetPassword);
		return R.status(temp);
	}

	/**
	 * @Description 修改密码
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param user
	 * @param oldPassword
	 * @param newPassword
	 * @param newPassword1
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update-password")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "修改密码", notes = "传入密码")
	//@PreAuth("hasAnyRole('administrator', 'user','test')")
	@ApiLog("修改用户密码")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	public R updatePassword(BladeUser user, @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
							@ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
							@ApiParam(value = "新密码", required = true) @RequestParam String newPassword1) {
		boolean temp = userService.updatePassword(user.getUserId(), oldPassword, newPassword, newPassword1);
		return R.status(temp);
	}

	/**
	 * @Description 用户列表
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R<java.util.List<com.dhcc.bpm.modules.system.entity.User>>
	 **/
	@GetMapping("/user-list")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "用户列表", notes = "传入user")
	@ApiLog("用户列表")
	@Log(title = "用户列表", businessType = BusinessType.SELECT)
	public R<List<User>> userList(User user) {
		List<User> list = userService.list(Condition.getQueryWrapper(user));
		return R.data(list);
	}
	/**
	 * @Description 新增或修改
	 * @Author sjm
	 * @Date 10:10 2020/2/22 0022
	 * @param user
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/updateInfo")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "新增或修改", notes = "传入User")
	//@PreAuth("hasAnyRole('administrator', 'user','test')")
	@CacheEvict(cacheNames = {USER_CACHE}, allEntries = true)
	@ApiLog("新增或修改用户")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	public R updateInfo(@Valid @RequestBody User user) {
		//修改用户信息
		if(user.getId()==null){
			return  R.fail("用户ID不存在，请重新登录!");
		}
		String avatar = user.getAvatar();
		// 定义正则表达式
		String regex = "/TXFile/(.*?)\\?";
		Pattern pattern = Pattern.compile(regex);

		// 创建 Matcher 对象
		Matcher matcher = pattern.matcher(avatar);
		// 检查是否匹配成功
		if (matcher.find()) {
			// 提取匹配到的内容
			String extractedFileName = matcher.group(1);
			user.setAvatar("TXFile/" + extractedFileName);
		} else {
			// 如果没有匹配成功，你可能需要处理这种情况，例如记录日志或者返回错误信息
			return R.fail("无法提取文件名，正则表达式未匹配成功");
		}
		return R.status(userService.updateUser(user));
	}
	/**
	 * @Description 查询板块下面的用户
	 * @Author daina
	 * @Date 10:24 2020/3/5
	 * @param user
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.UserVO>>
	 **/
	@GetMapping("/listTopicUser")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入metadata")
	@ApiLog("查询板块下面的用户")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	public R<IPage<UserVO>> listTopicUser(UserVO user, Query query) {
		IPage<UserVO> pages = userService.listTopicUser(Condition.getPage(query), user);
		return R.data(pages);
	}

	/**
	 * @Description 用户名关联查询
	 * @Author daina
	 * @Date 14:48 2020/2/25
	 * @param
	 * @return com.dhcc.bpm.common.json.JSONObject
	 **/
	@ApiLog("查询用户")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	@GetMapping("/selectUserName")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询用户", notes = "无参")
	public JSONObject selectBySysName() {
		JSONObject object = new JSONObject();
		object.put("success", true);
		object.put("names",userService.selectUserName());
		return object;
	}

	@ApiLog("查询用户")
	@Log(title = "查询",businessType = BusinessType.SELECT)
	@GetMapping("/selectUserNameByIds")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询用户名称", notes = "id")
	public JSONObject selectBySysName(@ApiParam(value = "主键", required = true) @RequestParam String id) {
		Alarm alarm = alarmService.getById(id);
		JSONObject object = new JSONObject();
		object.put("success",true);
		object.put("names",userService.selectUserNameByIds(Func.toLongList(alarm.getNoticeUser())));
		return object;
	}

}